package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"goods/models"
	"strconv"
)

type GoodsController struct {
	beego.Controller
}

type JsonRes struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *GoodsController) Add() {
	name := c.GetString("name")
	cate := c.GetString("cate")
	img := c.GetString("img")
	mio := c.GetString("mio")
	address := c.GetString("address")
	goods := models.Goods{
		Name:    name,
		Cate:    cate,
		Img:     img,
		Mio:     mio,
		Address: address,
	}
	err := models.DB.Create(&goods).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *GoodsController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "请重新选择文件",
			Data: nil,
		}
		c.ServeJSON()
	}
	defer file.Close()

	if header.Size > 2*1024*1024 {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "文件过大,不能超过2MB",
			Data: nil,
		}
		c.ServeJSON()
	}
	path := "static/img/" + header.Filename
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		imgUrl := "http://127.0.0.1:8080/" + path
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "上传成功",
			Data: imgUrl,
		}
		c.ServeJSON()
	}
}
func (c *GoodsController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	query := models.DB.Model(&models.Goods{})
	name := c.GetString("name")
	cate := c.GetString("cate")
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	if cate != "" {
		query = query.Where("cate = ?", cate)
	}
	key := "page-" + strconv.Itoa(page)
	val, err := models.Rdb.Get(key).Result()
	pageData := make(map[string]interface{})
	if err != nil {
		var count int64
		err = query.Count(&count).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "总数量查询失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		var goods []models.Goods
		offset := (page - 1) * size
		err = query.Limit(size).Offset(offset).Find(&goods).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		pageData["count"] = count
		pageData["goods"] = goods
		jsonByte, _ := json.Marshal(&pageData)
		value := string(jsonByte)
		models.Rdb.Set(key, value, 0)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "查询成功-mysql",
			Data: pageData,
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}
func (c *GoodsController) Delete() {
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Goods{}, id).Error
	if err != nil {
		c.Data["json"] = JsonRes{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *GoodsController) Info() {
	id, _ := c.GetInt("id")
	key := "goods-" + strconv.Itoa(id)
	val, err := models.Rdb.Get(key).Result()
	var goods models.Goods
	if err != nil {
		err = models.DB.Where("id=?", 1).First(&goods).Error
		if err != nil {
			c.Data["json"] = JsonRes{
				Code: 500,
				Msg:  "展示失败",
				Data: nil,
			}
			c.ServeJSON()
		}
		jsonByte, _ := json.Marshal(&id)
		value := string(jsonByte)
		models.Rdb.Set(key, value, 0)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "展示成功",
			Data: goods,
		}
		c.ServeJSON()
	} else {
		json.Unmarshal([]byte(val), &goods)
		c.Data["json"] = JsonRes{
			Code: 200,
			Msg:  "展示成功",
			Data: goods,
		}
		c.ServeJSON()
	}
}
